package cn.com.sixboy.service.base;

import cn.com.sixboy.cache.Cache;
import cn.com.sixboy.cache.Property;
import cn.com.sixboy.dao.QueryConditionEntity;
import cn.com.sixboy.dao.base.BaseDAO;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Sam.Zheng <zcl1866@sina.com>
 * @date 2013-4-25 16:47:33
 * @version <v0.1>
 * @copyright SixBoy
 */
@Service("baseService")
@Transactional
public class BaseServiceImplement<T> implements BaseService<T> {

    @Autowired
    protected BaseDAO<T> baseDAO;

    public BaseDAO<T> getBaseDAO() {
        return baseDAO;
    }

    public void setBaseDAO(BaseDAO<T> baseDAO) {
        this.baseDAO = baseDAO;
    }

    @Override
    // @Transactional(propagation=Propagation.REQUIRED)
    public boolean add(T entity) {
        return baseDAO.addEntity(entity);
    }

    @Override
    // @Transactional(propagation=Propagation.REQUIRED)
    public boolean delete(T entity) {
        return baseDAO.deleteEntity(entity);
    }

    @Override
    // @Transactional(propagation=Propagation.REQUIRED)
    public boolean update(T entity) {
        return baseDAO.updateEntity(entity);
    }

    @Override
    public T querySingleEntity(Class c, int primaryKey) {
        return baseDAO.querySingleEntity(c, primaryKey);
    }

    @Override
    public List queryListEntitiesByOneParam(String jpql, String param,
    		Object value) {
        return baseDAO.queryEntitiesByOneParam(jpql, param, value);
    }

    @Override
    public T queryEntityByOneParam(String jpql, String param, String value) {
        return baseDAO.queryEntityByOneParam(jpql, param, value);
    }

    @Override
    public List queryEntitiesByNativeSQL(String nativeSql, Class<?> type) {
        return baseDAO.queryEntitiesByNativeSQL(nativeSql, type);
    }

    @Override
    public List queryAllEntity(String jpql, boolean isNameQuery) {
        return baseDAO.queryAllEntity(jpql, isNameQuery);
    }

    @Override
    public boolean removeBetchEntity(List<T> entities) {
        return baseDAO.removeBetchEntity(entities);
    }

    @Override
    public boolean updateBatchEntity(List<T> entities) {
        return baseDAO.updateBatchEntity(entities);
    }

    @Override
    public List<T> queryEntities(QueryConditionEntity qce, boolean isNameQuery) {
        return baseDAO.queryEntities(qce, isNameQuery);
    }

    @Override
    public void clear() {
        baseDAO.clear();
    }

    @Override
    public Object getCacheData(String key) { //获取缓存数据
            Cache cache = Cache.getInstance();
            if (cache != null) {
                return cache.getCacheData().get(key);
            }
        return null;
    }
    @Override
    public void setCacheData(String key, Object data) { //设置缓存数据（静态数据使用效率高)
                Cache cache = Cache.getInstance();
                Map<String, Object> caches = cache.getCacheData();
                if (caches != null) {
                    caches.put(key, data);
                }
            }
   }
    

